package com.yyj.web.controller;

import com.yyj.common.BaseResponse;
import com.yyj.constant.BusinessConstants;
import com.yyj.dao.TPropertyPaymentMapper;
import com.yyj.enums.PaymentStatusEnum;
import com.yyj.enums.PaymentTypeEnum;
import com.yyj.model.TPropertyPayment;
import com.yyj.service.AppService;
import com.yyj.service.PaymentService;
import com.yyj.util.DateUtil;
import com.yyj.util.RspUtils;
import com.yyj.util.poi.POIUtil;
import com.yyj.web.protocol.req.PaymentAddReq;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.io.File;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

@ApiIgnore
@RestController
public class TestController extends BaseController{

    @Autowired
    PaymentService paymentService;

    @Autowired
    AppService appService;

    @Autowired
    TPropertyPaymentMapper paymentMapper;

    @RequestMapping(value = "/initFee", method = RequestMethod.GET)
    public void initFee(){
        try {
            paymentService.initManagePayment();

        } catch (Exception e) {
            logger.error(e.getMessage() , e);
        }
    }

    @RequestMapping(value = "/initAccessToken", method = RequestMethod.GET)
    public BaseResponse<? extends Object> initAccessToken(){
        try {
            appService.initAccessToken();
            return RspUtils.success();
        } catch (Exception e) {
            logger.error(e.getMessage() , e);
            return RspUtils.error("initAccessToken error : {}" , e.getMessage());
        }
    }

    @RequestMapping(value = "/initManageFee", method = RequestMethod.GET)
    public void initManageFee(){
        String userIdsFile = "f:/fee4.xlsx";
        try {
            StringBuffer userIdBuffer = new StringBuffer();
            StringBuffer orderNumBuffer = new StringBuffer();
            File file1 = new File(userIdsFile);
            List<String[]> list1 = POIUtil.readExcel(file1);
            list1 = list1.subList(0  , 3154);
            if (list1.size() > 0) {
                List<String[]> userIdList = list1;
                for(int i = 0 ; i < userIdList.size() ; i++) {
                    String roomNum = userIdList.get(i)[0].trim().replace("-", "");
                    String fee = userIdList.get(i)[1].trim();
                    String createDate = userIdList.get(i)[2].trim();
                    String feeDate = createDate.substring(0 , 4) + createDate.substring(5 , 7);

                    TPropertyPayment paymentAddReq = new TPropertyPayment();
                    paymentAddReq.setRoomNum(roomNum);
                    paymentAddReq.setAmount(new BigDecimal(fee));
                    paymentAddReq.setFeeDate(feeDate);
                    paymentAddReq.setStatus(PaymentStatusEnum.NO_PAY.getCode());
                    Date startTime = DateUtil.sdfLongTimePlus.parse(createDate);
                    paymentAddReq.setStartTime(startTime);
                    paymentAddReq.setCreateTime(startTime);
                    paymentAddReq.setUpdateTime(startTime);
                    paymentAddReq.setType(PaymentTypeEnum.MANAGE_FEE.getCode());
                    paymentMapper.insertSelective(paymentAddReq);

                }
            }
        }catch (Exception e){
            logger.error(e.getMessage() ,e);
        }
    }


    public static void main(String[] args){

        String userIdStr = "";
        String amountStr = "";
        String userIdsFile = "f:/fee111.xlsx";
        try {
            StringBuffer userIdBuffer = new StringBuffer();
            StringBuffer orderNumBuffer = new StringBuffer();
            File file1 = new File(userIdsFile);
            List<String[]> list1 = POIUtil.readExcel(file1);
            //list1 = list1.subList(0  , 815);
            if (list1.size() > 0) {
                List<String[]> userIdList = list1;
                int idIndex = 6455;

                for(int i = 0 ; i < userIdList.size() ; i++){
                    String roomNum = userIdList.get(i)[0].trim().replace("-", "");
                    String square = userIdList.get(i)[1].trim();
                    BigDecimal fee = new BigDecimal(Double.valueOf(square) * 0.8).setScale(2 , BigDecimal.ROUND_HALF_UP )
                            .add(new BigDecimal(BusinessConstants.REPAIRMENT_FEE))
                            .add(new BigDecimal(BusinessConstants.SANITARY_FEE))
                            ;
                    String createDateValue = userIdList.get(i)[2].trim();
                    Calendar calendar = new GregorianCalendar(1900,0,-1);
                    Date d = calendar.getTime();
                    Date dd = DateUtils.addDays(d,Integer.valueOf(createDateValue));
                    String year = (dd.getYear() + 1900) + "";
                    String month = (dd.getMonth() + 1) < 10 ? "0" + (dd.getMonth() + 1) : String.valueOf((dd.getMonth() + 1));
                    //String createDate = DateUtil.dateToString(dd , DateUtil.sdfLongTimePlus.toString());
                    String feeDate = year + month;
                    String status = PaymentStatusEnum.NO_PAY.getCode();
                    //Date startTime = DateUtil.sdfLongTimePlus.parse(createDate);
                    String startTime = year + "-" + month + "-" + (dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate()) + " 00:00:00";
                    String endTime =  DateUtil.getLastDayOfMonth(dd.getYear() + 1900 , dd.getMonth() + 1)+" 00:00:00";
                    String type = PaymentTypeEnum.MANAGE_FEE.getCode();

                    //System.out.println(roomNum + "---" + readNum2 + "-----" + finalDate);

                    String sql = "INSERT INTO `t_property_payment` VALUES " +
                            "('"+ idIndex++ +"', '"+ fee + "', '"+ roomNum +"', 'NO_PAY', 'MANAGE_FEE', '"+feeDate+"', null, '"+startTime+"', '"+startTime+"', null, null, null, null, null, null, null, '"+startTime+"', '"+endTime+"', null);";

                    System.out.println(sql);

                }
            }
        }catch (Exception e){
        }


    }
}
